layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 入库订单明细表管理
     */
    var OrderPutDetailed = {
        tableId: "orderPutDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    OrderPutDetailed.initColumn = function () {
        return [[
            {type: 'checkbox',fixed: 'left'},
            {field: 'id', hide: true, title: 'Primary Key',fixed: 'left'},
            {field: 'putDetailedNumber', sort: true, title: 'Inbound Order Number',fixed: 'left'},
            {field: 'detailedGoodsname', sort: true, title: 'Product Name'},
            {field: 'detailedFactory', sort: true, title: 'Factory Number'},
            {field: 'detailedPutbalenum', sort: true, title: 'Planned Large Packages'},
            {field: 'detailedPutnum', sort: true, title: 'Planned Quantity'},
            {field: 'detailedPutweight', sort: true, title: 'Planned Weight'},
            {field: 'detailedPutvolume', sort: true, title: 'Planned Volume'},
            {field: 'detailedUnitprice', sort: true, title: 'Unit Price'},
            {field: 'detailedMoney', sort: true, title: 'Amount'},
            {field: 'detailedBatchnumber', sort: true, title: 'Batch Number'},
            {field: 'detailedDateofmanufacture', sort: true, title: 'Production Date'},
            {field: 'detailedRebubbles', sort: true, title: 'Heavy/Bulky Goods'},
            {field: 'detailedPacking', sort: true, title: 'Product Packaging'},
            {field: 'detailedLargeclass', sort: true, title: 'Product Category'},
            {field: 'detailedGoodstype', sort: true, title: 'Product Type'},
            {field: 'detailedPutcondition', sort: true, title: 'Storage Conditions'},
            {field: 'detailedPiecenum', sort: true, title: 'Quantity per Package'},
            {field: 'nodes', sort: true, title: 'Remark'},
            {fixed: 'right',width: 165,align: 'center', toolbar: '#tableBar', title: 'Operation'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderPutDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(OrderPutDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     OrderPutDetailed.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     OrderPutDetailed.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     OrderPutDetailed.exportExcel();
                 });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    OrderPutDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/orderPutDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    OrderPutDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(OrderPutDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    OrderPutDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/orderPutDetailed/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OrderPutDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/orderPutDetailed/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(OrderPutDetailed.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderPutDetailed.tableId,
        url: Feng.ctxPath + '/orderPutDetailed/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: OrderPutDetailed.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderPutDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OrderPutDetailed.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OrderPutDetailed.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + OrderPutDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            OrderPutDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            OrderPutDetailed.onDeleteItem(data);
        }
    });
});
